package com.corgi.webservice.repository;

import com.corgi.webservice.domain.User;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: UserRepository
 * @description: 用户的仓储
 * @author: dengmiao
 * @create: 2019-10-11 17:33
 **/
@Repository
public class UserRepository {

	private Map<Long, User> cachedUser;

	@PostConstruct
	public void init() {
		cachedUser = new HashMap<Long, User>(16){
			{
				put(1L, createUser(1L, "张三", 18));
				put(2L, createUser(2L, "张三", 29));
				put(3L, createUser(3L, "王五", 24));
				put(4L, createUser(4L, "赵六", 21));
			}
		};
	}

	public User findById(long id) {
		return cachedUser.get(id);
	}

	public List<User> findUser() {
		return new ArrayList<>(cachedUser.values());
	}

	private User createUser(long id, String name, int age) {
		User user = new User();
		user.setId(id);
		user.setName(name);
		user.setAge(age);
		return user;
	}
}
